package com.wwz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.netflix.loadbalancer.RoundRobinRule;
import com.wwz.annoation.PageParam;
import com.wwz.constants.JsonResult;
import com.wwz.constants.LoginEnum;
import com.wwz.entity.UserEntity;
import com.wwz.service.impl.UserServiceImpl;
import com.wwz.tcc.TccUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

/**
 * @author wwz
 */
@Controller
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserServiceImpl userService;
    /*@Autowired
    private TccUserService tccUserService;*/
    @Autowired
    RestTemplate restTemplate;

  /*  @RequestMapping("/ins")
    @ResponseBody
    public Object insert(String name,long branchId){
      return  tccUserService.insert(null,name,branchId);
    }*/

    /**
     * 用户登录入口
     * @param user
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public Object getUser(@RequestBody UserEntity user){
        try{
            LoginEnum loginEnum = userService.login(user);
            return JsonResult.resolver(loginEnum);
        }catch (Exception e){

        }
        return JsonResult.resolver(LoginEnum.FAIL);
    }

    /**
     * 用户登录入口
     * @param user
     * @return
     */
    @RequestMapping("/loginTest")
    @ResponseBody
    public Object login(UserEntity user){
        try{
            LoginEnum loginEnum = userService.login(user);
            return JsonResult.resolver(loginEnum);
        }catch (Exception e){

        }
        return JsonResult.resolver(LoginEnum.FAIL);
    }

    /**
     * 查询用户列表
     * @param page
     * @return
     */
    @RequestMapping("/user/list")
    @ResponseBody
    public Object userList(@PageParam() Page page){
        try{
            return JsonResult.resolverPage(userService.selectPage(page,new EntityWrapper<>()));
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.resolverPageError(null);
        }
    }
}
